package cn.iocoder.yudao.module.call.service.cmoutnumber;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.api.tenant.TenantApi;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.call.controller.admin.cmoutnumber.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.cmoutnumber.CmOutnumberDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.call.dal.mysql.cmoutnumber.CmOutnumberMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.call.enums.ErrorCodeConstants.OUT_NUMBER_NOT_CONFIG;

/**
 * 外呼号码 Service 实现类
 *
 * @author 数创万维
 */
@Service
@Validated
public class CmOutnumberServiceImpl implements CmOutnumberService {

    @Resource
    private CmOutnumberMapper cmOutnumberMapper;

    @Resource
    private TenantApi tenantApi;

    @Override
    public Long createCmOutnumber(CmOutnumberSaveReqVO createReqVO) {
        // 插入
        CmOutnumberDO exists = cmOutnumberMapper.selectOne(new LambdaQueryWrapper<CmOutnumberDO>().eq(CmOutnumberDO::getDisplayNumber, createReqVO.getDisplayNumber()).last("limit 1"));
        if (ObjectUtil.isNotEmpty(exists)) {
            throw exception(new ErrorCode(100500, "该号码已存在"));
        }
        CmOutnumberDO outNumberExists = cmOutnumberMapper.selectOne(new LambdaQueryWrapper<CmOutnumberDO>().eq(CmOutnumberDO::getOutNumber, createReqVO.getOutNumber()).last("limit 1"));
        if (ObjectUtil.isNotEmpty(outNumberExists)) {
            throw exception(new ErrorCode(100500, "该号码前缀已存在"));
        }

        CmOutnumberDO cmOutnumber = BeanUtils.toBean(createReqVO, CmOutnumberDO.class);
        //填充cm对应租户
        cmOutnumber.setTenantId(NumberUtil.parseLong(SpringUtil.getProperty("call.cm.tenant-id")));
        cmOutnumberMapper.insert(cmOutnumber);
        // 返回
        return cmOutnumber.getId();
    }

    @Override
    public void updateCmOutnumber(CmOutnumberSaveReqVO updateReqVO) {
        // 更新
        CmOutnumberDO exists = cmOutnumberMapper.selectOne(new LambdaQueryWrapper<CmOutnumberDO>().eq(CmOutnumberDO::getDisplayNumber, updateReqVO.getDisplayNumber()).last("limit 1"));
        if (ObjectUtil.isNotEmpty(exists) && !Objects.equals(exists.getId(), updateReqVO.getId())) {
            throw exception(new ErrorCode(100500, "该号码已存在"));
        }
        CmOutnumberDO outNumberExists = cmOutnumberMapper.selectOne(new LambdaQueryWrapper<CmOutnumberDO>().eq(CmOutnumberDO::getOutNumber, updateReqVO.getOutNumber()).last("limit 1"));
        if (ObjectUtil.isNotEmpty(outNumberExists)&& !Objects.equals(outNumberExists.getId(), updateReqVO.getId())) {
            throw exception(new ErrorCode(100500, "该号码前缀已存在"));
        }
        CmOutnumberDO updateObj = BeanUtils.toBean(updateReqVO, CmOutnumberDO.class);
        cmOutnumberMapper.updateById(updateObj);
    }

    @Override
    public void deleteCmOutnumber(Long id) {
        // 删除
        cmOutnumberMapper.deleteById(id);
    }

    @Override
    public List<CmOutnumberDO> getTenantCmOutnumber() {
        List<Long> numberIdList = tenantApi.getCallNumberList(TenantContextHolder.getTenantId());
        if (ObjectUtil.isEmpty(numberIdList)) {
            throw exception(OUT_NUMBER_NOT_CONFIG);
        }
        return cmOutnumberMapper.selectList(new LambdaQueryWrapper<CmOutnumberDO>().in(CmOutnumberDO::getId, numberIdList));
    }

    @Override
    public CmOutnumberDO getCmOutnumber(Long id) {
        return cmOutnumberMapper.selectById(id);
    }

    @Override
    public PageResult<CmOutnumberDO> getCmOutnumberPage(CmOutnumberPageReqVO pageReqVO) {
        return cmOutnumberMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CmOutnumberDO> getCmOutnumberList() {
        return cmOutnumberMapper.selectList();
    }

    @Override
    public CmOutnumberDO getCmOutnumberByDisplayNumber(String number) {
        return cmOutnumberMapper.selectOne(CmOutnumberDO::getDisplayNumber, number);

    }
}